import { Component, ChangeDetectorRef, ChangeDetectionStrategy } from "@angular/core";
import { Subscription } from "rxjs";
import { CompanyService } from "../../company.service";
import { CompanyControversiesService } from "../companyControversies.service";
import { IncidentCategory } from "../../../../../shared/models/incidentCategory.enum";
import { environment } from "../../../../../../environments/environment";
import { CompanyBaseWidget } from "../../shared/companyBaseWidget/companyBase.widget";
import { ActivatedRoute, Router } from "@angular/router";

/**
 * Controversies widget component
 * 
 * Author: Sorin Spiru
 * Date: 04.03.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: './widget.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class WidgetComponent extends CompanyBaseWidget {
    private companySub: Subscription;
    private controversiesSub: Subscription;
    public maxAssesment: number;
    public incidentCategory: typeof IncidentCategory = IncidentCategory;
    public resourcesUrl: string = environment.resourcesUrl;

    constructor(
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService,
        private companyControversiesService: CompanyControversiesService) {
        super(route, router);
    }

    ngOnInit() {
        this.companySub = this.companyService.company.subscribe(company => {
            this.controversiesSub = this.companyControversiesService.getControversiesByTheme(company.id).subscribe(data => {
                this.maxAssesment = Math.max.apply(Math, data.map(x => { return x.answerCategory; }));
                this.cdRef.markForCheck();
            });
        });
    }

    ngOnDestroy() {
        this.companySub.unsubscribe();
        this.controversiesSub.unsubscribe();
    }
}